#pragma once
#include <Windows.h>
#include <string>
#include <queue>

extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libavutil/imgutils.h"
#include "libswresample/swresample.h"
};
#include "qframe.h"

class VideoDecode
{
public:
    VideoDecode();
    virtual ~VideoDecode();

    int InitDecode(const std::string& fileName, AVPixelFormat m_pixFormat, int maxCache);
    int DecodeStart();
    int DecodeStop();

    int DecodeThread();
    bool HavePic()
    {
        return true;
    }
    double GetTimeBase()
    {
        return m_timeBase;
    }
    AVFrame* GetShowFrame();
    int NextFrame();
    int Seek(int sec);

    double GetShowTime()
    {
        return m_showtime;
    }
    int GetCountTime()
    {
        return m_countTime;
    }
    std::string GetFilePath()
    {
        return m_filepath;
    }
    int GetWidth() const { return m_width; };
    int GetHeight() const { return m_height; };

    static unsigned int __stdcall WorkingThread(void *lp);

private:
    bool m_run{ false };
    bool m_seek{ false };
    std::string m_filepath;
    int m_width{ 0 };
    int m_height{ 0 };
    int m_maxCache{ 100 };
    double m_timeBase{ 0 };
    double m_time{ 0 };
    int m_countTime{ 0 };
    int videoindex{ -1 };
    enum{DECODE, DECODE_INIT, DECODE_RUNING, DECODE_END};
    int m_decode{ DECODE };
    AVPixelFormat m_pixFormat{ AV_PIX_FMT_YUV420P };//AV_PIX_FMT_YUV420P;
    AVFormatContext *m_pFormatCtx{ NULL };
    AVCodecContext *m_pCodecCtxVideo{NULL};
    SwsContext *m_pImgConvertCtx{ NULL };
    
    //AVFrame	*pFrameYUV ;
    // frame queue
private:
    std::queue<AVFrame*> m_poolFrame;
    std::queue<AVFrame*> m_cacheFrame;
    AVFrame* m_showFrame{ NULL };
    double m_showtime{ 0 };
    //QVFrame m_queue;

    HANDLE m_thread{NULL};

};

